﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.ApplicationModel;
using Luna.WPF.ApplicationFramework;
using Caliburn.Core.Metadata;
using Microsoft.Practices.ServiceLocation;
using XWord.Fundamental.Domain;
using System.ComponentModel;
using System.Windows.Data;
using XWord.WPF.ApplicationFramework.Threads;
using XWord.GUI.Presenters.Interface;

namespace XWord.GUI.Presenters.Words
{
    [Singleton(typeof(IWordExplainPresenter))]
    public class WordRelateEtymaPresenter : WordExplainPresenter, IWordExplainPresenter
    {
        public WordRelateEtymaPresenter()
        {
            this.DisplayName = "相关词根词缀";
            this.Index = 2;
        }

        public override void WordChanged(string word)
        {
            SearchWordEtyma(word);
        }

        public IEtymaLibraryExplorerPresenter EtymaLibraryExplorer { get; set; }

        public void SearchWordEtyma(string text)
        {

            if (string.IsNullOrEmpty(text)) return;
            EtymaLibraryList = EtymaLibraryExplorer.AllEtymaLibraryList.
                    Where(e => EtymaUtil.ParseText(e.Name).Any(m =>
                       (text.Contains(m.Name))
                        )).OrderBy(e => e.EtymaType).ToList();
            Refresh();
        }

        private void Refresh()
        {
            UIThread.BeginInvoke(() =>
            {
                var view = CollectionViewSource.GetDefaultView(EtymaLibraryList);
                view.GroupDescriptions.Clear();
                view.GroupDescriptions.Add(new PropertyGroupDescription("EtymaType", new EtymaTypeCoverter()));
            });
        }

        public IList<EtymaLibrary> _etymaLibraryList;
        public IList<EtymaLibrary> EtymaLibraryList
        {
            get { return _etymaLibraryList ?? (_etymaLibraryList = new List<EtymaLibrary>()); }
            set
            {
                _etymaLibraryList = value;
                NotifyOfPropertyChange("EtymaLibraryList");
            }
        }
    }
}
